package cn.fxl.user.controller;

import cn.fxl.basic.exception.BusinessExciption;
import cn.fxl.basic.result.AjaxResult;
import cn.fxl.user.dto.LoginDto;
import cn.fxl.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 用户登录
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILogininfoService iLogininfoService;

    /**
     * 后台账号登录
     * @param loginDto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){
        try {
           return iLogininfoService.accountLogin(loginDto);
        } catch (BusinessExciption e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }
    }

    /**
     * 微信登录
     * @param map
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechatLogin(@RequestBody Map<String,String> map){
        try {
            return iLogininfoService.wechatLogin(map);
        } catch (BusinessExciption e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }
    }

    /**
     * 微信第一次绑定登录
     * @param map
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody Map<String,String> map){
        try {
            return iLogininfoService.wechatBinder(map);
        } catch (BusinessExciption e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }
    }
}
